﻿using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMapperSamples.Samples
{
    /// <summary>
    /// 条件映射
    /// </summary>
    [TestClass]
    public class ConditionalMapping
    {
        [TestMethod]
        public void ConditionalTest()
        {
            Mapper.Initialize(cfg => {
                cfg.CreateMap<Foo, Bar>()
                  .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz > 1)));
            });
            var srcFoo = new Foo { baz =2 };
            var destBar = Mapper.Map<Foo, Bar>(srcFoo);
            Assert.AreEqual(2, destBar.baz);

            var srcFoo1 = new Foo { baz = 1};
            var destBar2 = Mapper.Map<Foo, Bar>(srcFoo1);
            Assert.AreEqual(0, destBar2.baz);
        }
    }

    public class Foo
    {
        public int baz;
    }

    public class Bar
    {
        public int baz;
    }
}
